﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using DataBase;

namespace DBService
{
	public class DbElementForRemove : DbObject
	{
		[Category("属性")]
		[Description("模构件分类")]
		[DisplayName("构件分类")]
		[ReadOnly(true)]
		public ElementCategory Category
		{
			get
			{
				return this.mCategory;
			}
			set
			{
				this.mCategory = value;
			}
		}

		[Category("属性")]
		[Description("模型Id")]
		[DisplayName("结构模型Id")]
		[ReadOnly(true)]
		public int ElementId
		{
			get
			{
				return this.mElementId;
			}
			set
			{
				this.mElementId = value;
			}
		}

		[Category("属性")]
		[Description("构件类型")]
		[DisplayName("构件类型")]
		[ReadOnly(true)]
        public DBService.ElementType ElementType
		{
			get
			{
				return this.mElementType;
			}
			set
			{
				this.mElementType = value;
			}
		}

		[Category("属性")]
		[Description("模型路径")]
		[DisplayName("模型路径")]
		[ReadOnly(true)]
		public string FilePath
		{
			get
			{
				DbProject fromDB = DbProject.GetFromDB(this.mProjectId, BimServerDB.Default);
				string result = "";
                if (this.mElementType == DBService.ElementType.Rac)
				{
					result = fromDB.RacFilePath;
				}
                else if (this.mElementType == DBService.ElementType.Rst)
				{
					result = fromDB.RstFilePath;
				}
                else if (this.mElementType == DBService.ElementType.Rme)
				{
					result = fromDB.RmeFilePath;
				}
				return result;
			}
		}

        public DbElementForRemove(int projectId, int elementId, DBService.ElementType elementType, ElementCategory category)
		{
			this.mProjectId = projectId;
			this.mElementId = elementId;
			this.mElementType = elementType;
			this.mCategory = category;
		}

		public static void Clear(DbProject project)
		{
			foreach (DbElementForRemove dbElementForRemove in DbElementForRemove.GetByProject(project))
			{
				dbElementForRemove.RemoveFromDb();
			}
		}

		public static List<DbElementForRemove> GetByProject(DbProject project)
		{
			List<DbElementForRemove> result;
			try
			{
				if (project.Database.Connection.State == ConnectionState.Closed)
				{
					project.Database.Connection.Open();
				}
				List<DbElementForRemove> list = new List<DbElementForRemove>();
				string sql = string.Format("SELECT Id,ProjectId,ElementId,ElementType,ElementCategory FROM tbElementToRemove WHERE ProjectId={0}", project.Id);
				DataTable dataTable = project.Database.ExecQuery(sql);
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				if (dataTable == null)
				{
					result = null;
				}
				else
				{
					for (int i = 0; i < dataTable.Rows.Count; i++)
					{
						int mId = Convert.ToInt32(dataTable.Rows[i]["Id"]);
						int projectId = Convert.ToInt32(dataTable.Rows[i]["ProjectId"]);
						int elementId = Convert.ToInt32(dataTable.Rows[i]["ElementId"]);
						int elementType = Convert.ToInt32(dataTable.Rows[i]["ElementType"]);
						ElementCategory category = (ElementCategory)Convert.ToInt32(dataTable.Rows[i]["ElementCategory"]);
                        list.Add(new DbElementForRemove(projectId, elementId, (DBService.ElementType)elementType, category)
						{
							mId = mId,
							mDatabase = project.Database
						});
					}
					result = list;
				}
			}
			catch (Exception ex)
			{
				if (project.Database.Connection.State == ConnectionState.Open)
				{
					project.Database.Connection.Close();
				}
				throw ex;
			}
			return result;
		}

		public override void AddToDb(IDatabase db)
		{
			try
			{
				if (db.Connection.State == ConnectionState.Closed)
				{
					db.Connection.Open();
				}
				string sql = string.Format("INSERT INTO tbElementToRemove (ProjectId, ElementId,ElementType,ElementCategory) VALUES ({0},{1},{2},{3})", new object[]
				{
					this.mProjectId,
					this.mElementId,
					this.mElementType,
					this.mCategory
				});
				db.ExecuteCommand(sql);
				string sql2 = string.Format("select max(Id) from tbElementToRemove", Array.Empty<object>());
				DataTable dataTable = db.ExecQuery(sql2);
				this.mId = Convert.ToInt32(dataTable.Rows[0][0]);
				this.mDatabase = db;
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (db.Connection.State == ConnectionState.Open)
				{
					db.Connection.Close();
				}
				throw ex;
			}
		}

		public override void UpdateToDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("UPDATE tbElementToRemove SET ProjectId = {0},ElementId={1},ElementType={2},ElementCategory={3} WHERE Id ={4}", new object[]
				{
					this.mProjectId,
					this.mElementId,
					(int)this.mElementType,
					(int)this.mCategory,
					base.Id
				});
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override void RemoveFromDb()
		{
			try
			{
				if (base.Database.Connection.State == ConnectionState.Closed)
				{
					base.Database.Connection.Open();
				}
				string sql = string.Format("DELETE FROM tbElementToRemove WHERE Id = {0}", base.Id);
				base.Database.ExecuteCommand(sql);
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
			}
			catch (Exception ex)
			{
				if (base.Database.Connection.State == ConnectionState.Open)
				{
					base.Database.Connection.Close();
				}
				throw ex;
			}
		}

		public override string ToString()
		{
			string result = "";
            if (this.mElementType == DBService.ElementType.Rac)
			{
				result = "建筑-" + this.mElementId.ToString();
			}
            else if (this.mElementType == DBService.ElementType.Rst)
			{
				result = "结构-" + this.mElementId.ToString();
			}
            else if (this.mElementType == DBService.ElementType.Rme)
			{
				result = "水暖电-" + this.mElementId.ToString();
			}
			return result;
		}

		private int mProjectId;

		private ElementCategory mCategory;

		private int mElementId;

        private DBService.ElementType mElementType;
	}
}
